//
//  ReadListInfoViewController.m
//  Leisure
//
//  Created by Alex on 16/6/30.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "ReadListInfoViewController.h"

@interface ReadListInfoViewController ()<UIWebViewDelegate>
@property (nonatomic,strong)UIWebView *webView;
@property (nonatomic,strong)NSString *html;
@end

@implementation ReadListInfoViewController

- (UIWebView *)webView {
    
    if (!_webView) {
        _webView = [[UIWebView alloc]initWithFrame:self.view.frame];
        _webView.delegate = self;
        _webView.scrollView.bounces = NO;
        _webView.scrollView.bouncesZoom = NO;
    }
    return _webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.webView];
    
 //   self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(itemAction)];
   
    
    // 请求数据
    [RequestManager fecthDataWithUrl:kReadListInfoUrl parameter:@{@"contentid":self.readListModel.ID} mehtod:@"POST" compelet:^(NSData *data) {
        
        NSDictionary *bigdic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",bigdic[@"data"][@"html"]);
        // 根据pre进行分割
        NSArray *array = [bigdic[@"data"][@"html"] componentsSeparatedByString:@"<pre>"];
        
        [self.webView loadHTMLString:array[0] baseURL:nil];
        self.html = array[0];
        
    } updateUI:^{
       
        
    }];
    
 //   [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
    
   
}

#pragma mark ----------WebViewDelegate-----------
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString *js = @"function imgAutoFit() { \
    var imgs = document.getElementsByTagName('img'); \
    for (var i = 0; i < imgs.length; ++i) {\
    var img = imgs[i];   \
    img.style.maxWidth = %f;   \
    } \
    }";
    js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width - 20];
    
    [webView stringByEvaluatingJavaScriptFromString:js];
    [webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction)];
}

-(void)webViewDidStartLoad:(UIWebView *)webView {
    
    NSLog(@"开始加载");
    
}

// 收藏事件
- (void)itemAction {
    
    [[CoreDataManager shareCoreDataManager]insertCollectionModelWithReadModel:self.readListModel html:self.html];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}



@end
